package mammuthus.template

import mammuthus.master.Master
import com.google.inject.{Singleton, Inject}
import net.csdn.modules.transport.HttpTransportService
import scala.collection.JavaConversions._
import net.csdn.common.path.Url
import net.liftweb.json._
import mammuthus.protocal.AppMessage


/**
 * 5/3/15 WilliamZhu(allwefantasy@gmail.com)
 */
@Singleton
class ServiceDiscover @Inject() (master:Master,http:HttpTransportService) {
  implicit val formats = DefaultFormats

  def find(appName:String):Option[AppMessage] = {
    val response = http.get(new Url(s"http://${master.masterHost}:${master.masterPort}/app/info"),Map("appName"->appName))
    if(response == null || response.getStatus != 200) return None
    Some(parse(response.getContent).extract[AppMessage])
  }

}
